package simple_bbs.model;

import act.db.morphia.MorphiaAdaptiveRecord;
import act.db.morphia.MorphiaDao;
import act.social.SocialProfile;
import org.mongodb.morphia.annotations.Entity;

@Entity("user")
public class User extends MorphiaAdaptiveRecord<User> implements UserLinked {

    public static final User ANONYMOUS = buildAnonymous();

    public String email;
    public String displayName;
    public String fullName;

    private User() {
    }

    public User(SocialProfile profile) {
        email = profile.getEmail();
        displayName = profile.getDisplayName();
        fullName = profile.getFullName();
        putValues(profile.getAttributes());
    }

    @Override
    public String userId() {
        return email;
    }

    public static class Dao extends MorphiaDao<User> {

        public User findByEmail(String email) {
            return findOneBy("email", email);
        }

    }

    private static User buildAnonymous() {
        User user = new User();
        user.fullName = "anonymous";
        user.email = "who@where.com";
        user.putValue("html_url", "https://github.com");
        return user;
    }

}
